﻿Imports System.Web

Namespace AboutDNN.DNNMVC


    Public Class ErrorViewModel

        Public ErrorMessage As String
        Public ErrorDetail As String
        Public ReturnUrl As String

        Public Shared Function Create(ByVal ex As Exception) As ErrorViewModel
            Return Create(ex, App.MVCContext.Route.GetUrl)
        End Function

        Public Shared Function Create(ByVal ex As Exception, ByVal ReturnUrl As String) As ErrorViewModel
            Dim msg = ""
            Dim detail = ""
            If ex IsNot Nothing Then
                If ex.InnerException IsNot Nothing Then
                    msg = ex.InnerException.Message
                    detail = ex.InnerException.ToString
                Else
                    msg = ex.Message
                    detail = ex.ToString
                End If
            Else
                msg = "An unknown error has ocurred!"
            End If
            Return Create(msg, detail, ReturnUrl)
        End Function

        Public Shared Function Create(ByVal ErrorMessage As String, ByVal ErrorDetail As String, ByVal ReturnUrl As String) As ErrorViewModel
            Dim co = New ErrorViewModel
            co.ErrorMessage = HttpUtility.HtmlEncode(ErrorMessage)

            ' encode and replace the return char with <br /> for display
            ErrorDetail = HttpUtility.HtmlEncode(ErrorDetail)
            ErrorDetail = ErrorDetail.Replace(vbCrLf, "<br />")

            co.ErrorDetail = ErrorDetail
            co.ReturnUrl = ReturnUrl
            Return co
        End Function

    End Class

End Namespace